package day5;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @Author juzipi
 * @Date 2021/6/8 20:37
 * @Info 家庭收支小demo
 */
public class FamilyPractice {

    private static final List<Integer> money = new ArrayList<>();

    public static void main(String[] args) {
        String detailsTemplate = "收支        "+"     账户金额        "+"     收支金额        "+"     说明";
        String details = "收支\t  账户金额\t  收支金额\t  说 明\n";
        int balance = 1000;

        while (true){
            System.out.println("----------家庭收支记账----------");
            System.out.println("          1.收支明细");
            System.out.println("          2.登记收入");
            System.out.println("          3.登记支出");
            System.out.println("          4.退   出");
            Scanner scanner = new Scanner(System.in);
            System.out.println("请选择:");
            String s = scanner.nextLine();
            switch (s){
                case "1":
                    System.out.println("收支明细");
                    System.out.println(detailsTemplate);
                    System.out.println(details);
                    System.out.println("--------------------------------------");
                    break;
                case "2":
                    System.out.println("本次收入金额");
                    int i = readNumber();
                    System.out.println(i);
                    System.out.println("本次收入说明");
                    String string = readString();
                    System.out.println(string);
                    balance += i;
                    details += " 账户金额\t "+balance+" 收支金额\t "+i+" 说 明\t" + string +"\n";
                    break;
                case "3":
                    System.out.println("登记支出");
                    break;
                case "4":
                    System.out.println("是否退出(y/n)");
                    Scanner scanner1 = new Scanner(System.in);
                    String nextLine = scanner1.nextLine();
                    String logout = logout(nextLine);
                    if (logout.equals("退出成功")){
                        System.out.println(logout);
                        System.exit(0);
                    }
                    System.out.println(logout);
                default:
                    System.out.println("操作错误");
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    break;
            }
        }
    }

    private static String logout(String y){
        if (y.equals("y")){
            return "退出成功";
        }
        if (y.equals("n")){
            return "取消退出";
        }
        return "输入错误";
    }

    private static int readNumber(){
        Scanner keyWord = getKeyWord();
        money.add(keyWord.nextInt());
        if (money.size() != 0){
            for (int i = 0; i < money.size(); i++) {
                Integer integer = money.get(i);
                return integer;
            }
        }
        return 0;
    }

    private static String readString(){
        Scanner keyWord = getKeyWord();
        String s = keyWord.nextLine();
        return s;
    }


    private static Scanner getKeyWord(){
        return new Scanner(System.in);
    }

}
